<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评论管理</el-breadcrumb-item>
      <el-breadcrumb-item>评论列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图-->
    <el-card>
      <!--搜索与添加-->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input @clear="getArticleList" clearable v-model="queryInfo.query" placeholder="请输入内容">
            <el-button @click="getArticleList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--文章表格-->
      <el-table :data="articleList" >
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="文章名称" prop="title" width='120px'></el-table-column>
        <!--操作按钮-->
        <el-table-column label="评论" >
          <template slot-scope="scope">
             <el-table :data="scope.row.comments" height="250" stripe border>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column label="名称" prop="name" width='120px'>
                  <template slot-scope="scope">
                    <el-tag type="success">{{scope.row.name}} 说： </el-tag>
                  </template>
                </el-table-column>
                 <el-table-column label="评论内容">
                  <template slot-scope="scope">
                    <el-tag >{{scope.row.comments}} </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="评论时间">
                  <template slot-scope="scope">
                    <el-tag >{{scope.row.create_time|timeFormat}} </el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="操作">
                  <template slot-scope="scope">
                     <el-switch
                      :active-value="!scope.row.isShow"
                      @change="changeIsShow(scope.row.isShow,scope.row.id)"
                      inactive-text="关闭"
                      active-text="显示">
                    </el-switch>
                    <el-button
                      style="margin-left:10px;"
                      type="danger"
                      size="mini"
                      icon="el-icon-delete"
                      @click="() => remove(scope.row.id)">删除
                    </el-button>
                  </template>
                </el-table-column>
             </el-table>

          </template>
        </el-table-column>
      </el-table>
      <!--分页按钮-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

  </div>
</template>

<script>
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import { quillEditor } from "vue-quill-editor";
import { log } from 'util';
import {getArticleClass, getArticleList} from '@/api/article';
import {deleteComments, getComments, putComments} from '@/api/comment';
import {timeFormat} from '@/utils/utils';
export default {
  name: "Users",
  components: {
    quillEditor,
  },
  data() {
    const data = [{
        id: 1,
        label: '一级 1',
      }, {
        id: 2,
        label: '一级 2',
      }, {
        id: 3,
        label: '一级 3',
      }];
    return {
        data: JSON.parse(JSON.stringify(data)),
      //分类列表
      editorOption: {
        // some quill options
      },
      //获取文章列表的参数对象
      queryInfo: {
        query: "",
        //当前页数
        pagenum: 1,
        //当前每页显示多少条数据
        pagesize: 10,
      },
      articleList:[],
      total: 0,
      //编辑文章表单
      editForm: {
        title: "",
        desb: "",
        class_id: "",
        content: "",
      },
    };
  },
  created() {
    this.getArticleList();
  },
  methods: {
     remove(id){
       this.$confirm('此操作将永久删除该评论, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async() => {
          const res = await deleteComments(id)
          if(res.meta.status>=300) return  this.$message.error('删除失败',res.meta.msg);
          this.$message.success("删除成功");
          this.getArticleList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    async changeIsShow(isShow,id){
        const res= await putComments(id,{
          isShow:!isShow
        })
        if(res.meta.status>=300) return  this.$message.error('修改失败',res.meta.msg);
        this.$message.success("修改成功");
        this.getArticleList()
    },
    //获取文章列表
    async getArticleList() {
      const res= await getArticleList("article/list", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.articleList = res.data;
      this.total = res.data.total;

      const promiseArray=[]
      for (const item of this.articleList) {
        promiseArray.push(getComments(item.id))
      }
      const comments=await Promise.all(promiseArray)
      let ans=[]
      for (let i = 0; i < comments.length; i++) {
        ans=comments.map((item,index)=>({
          ...this.articleList[index],
          comments:item.data
        }))
      }
      this.articleList=ans
      console.log(ans);
    },

    //监听 pagesize改变事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getArticleList();
    },
    //监听页码值改变事件
    handleCurrentChange(newPage) {
      console.log(newPage);
    },

    //获取分类列表
    async getClassList() {
      const res = await getArticleClass(`article/class`);
      console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("设获取分类列表失败" + res.meta.msg);
      this.classList = res.data;
    },
  },
  filters: {
    timeFormat,
    class_name(class_id) {
      const class_item = this.classList.find((item) => {
        return item.id == class_id;
      });
      return class_item.class_name;
    },
  },
};
</script>

<style lang="less" scoped>
</style>
